﻿using System;
using System.IO;
using UnityEngine;
using UnityEngine.UI;

namespace Studio
{
	// Token: 0x02000553 RID: 1363
	[Serializable]
	public class UI_ColorPresets : UI_ColorInfo
	{
		// Token: 0x060022C8 RID: 8904 RVA: 0x000D1E54 File Offset: 0x000D0054
		protected override void Init()
		{
			base.Init();
			this.LoadCustomPresets();
		}

		// Token: 0x060022C9 RID: 8905 RVA: 0x000D1E64 File Offset: 0x000D0064
		public void OnPushPresetsSampleColor(Image img)
		{
			if (null == img)
			{
				return;
			}
			base.SetColor(img.color, UI_ColorInfo.ControlType.PresetsSample);
		}

		// Token: 0x060022CA RID: 8906 RVA: 0x000D1E84 File Offset: 0x000D0084
		public void OnPushPresetsCustomColor(Image img, int lr)
		{
			if (lr == -1 || null == img)
			{
				return;
			}
			Image image = null;
			Transform transform = img.gameObject.transform.Find("Image");
			if (transform)
			{
				image = transform.GetComponent<Image>();
			}
			if (lr == 0)
			{
				Color color = img.color;
				if (image)
				{
					color.a = 1f - image.color.a;
				}
				base.SetColor(color, UI_ColorInfo.ControlType.PresetsSample);
			}
			else if (lr == 1)
			{
				Color colorRGB = base.GetColorRGB();
				colorRGB.a = 1f;
				img.color = colorRGB;
				if (image)
				{
					Color color2 = image.color;
					color2.a = 1f - base.GetColorAlpha();
					image.color = color2;
				}
				this.SaveCustomPresets();
			}
		}

		// Token: 0x060022CB RID: 8907 RVA: 0x000D1F68 File Offset: 0x000D0168
		private void SaveCustomPresets()
		{
			string path = UserData.Path + "Save/ColorPresets.dat";
			using (FileStream fileStream = new FileStream(path, FileMode.Create, FileAccess.Write))
			{
				using (BinaryWriter binaryWriter = new BinaryWriter(fileStream))
				{
					for (int i = 0; i < 48; i++)
					{
						Color color = Color.white;
						if (this.objCustomColor[i])
						{
							Image component = this.objCustomColor[i].GetComponent<Image>();
							if (component)
							{
								color = component.color;
							}
							Transform transform = this.objCustomColor[i].transform.Find("Image");
							if (transform)
							{
								Image component2 = transform.GetComponent<Image>();
								if (component2)
								{
									color.a = component2.color.a;
								}
							}
						}
						binaryWriter.Write((double)color.r);
						binaryWriter.Write((double)color.g);
						binaryWriter.Write((double)color.b);
						binaryWriter.Write((double)color.a);
					}
				}
			}
		}

		// Token: 0x060022CC RID: 8908 RVA: 0x000D20B0 File Offset: 0x000D02B0
		private void LoadCustomPresets()
		{
			string path = UserData.Path + "Save/ColorPresets.dat";
			if (!File.Exists(path))
			{
				return;
			}
			using (FileStream fileStream = new FileStream(path, FileMode.Open, FileAccess.Read))
			{
				using (BinaryReader binaryReader = new BinaryReader(fileStream))
				{
					for (int i = 0; i < 48; i++)
					{
						Color white = Color.white;
						white.r = (float)binaryReader.ReadDouble();
						white.g = (float)binaryReader.ReadDouble();
						white.b = (float)binaryReader.ReadDouble();
						white.a = (float)binaryReader.ReadDouble();
						if (this.objCustomColor[i])
						{
							Image component = this.objCustomColor[i].GetComponent<Image>();
							if (component)
							{
								component.color = new Color(white.r, white.g, white.b, 1f);
							}
							Transform transform = this.objCustomColor[i].transform.Find("Image");
							if (transform)
							{
								Image component2 = transform.GetComponent<Image>();
								if (component2)
								{
									component2.color = new Color(1f, 1f, 1f, white.a);
								}
							}
						}
					}
				}
			}
		}

		// Token: 0x040023E8 RID: 9192
		private const int customColorNum = 48;

		// Token: 0x040023E9 RID: 9193
		public GameObject[] objCustomColor = new GameObject[48];
	}
}
